#!/usr/bin/env python3

# Copyright (c) 2020-2021 Fpemud <fpemud@sina.com>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

import os
from ._errors import PrepareError


class RunnerStrait:
    pass


class RunnerStraitNeedCacheDir(RunnerStrait):

    def __init__(self, game_id):
        self._path = os.path.normpath(os.path.expanduser(os.path.join("~", ".cache", game_id)))

    def _prepare(self, context):
        if not os.path.exists(self._path):
            try:
                os.mkdir(self._path)
            except OSError as e:
                raise PrepareError("failed to create cache directory \"%s\", %s" % (self._path, e))
        else:
            if not os.path.isdir(self._path):
                raise PrepareError("\"%s\" is not a directory" % (self._path))

        context.cache_dirpath = self._path

    @property
    def cache_dirpath(self):
        return self._path


class RunnerStraitHasCacheDir(RunnerStrait):

    def __init__(self, game_object):
        self._path = game_object.get_cache_dir()

    def _prepare(self, context):
        # game creates cache directory itself
        if os.path.exists(self._path) and not os.path.isdir(self._path):
            raise PrepareError("game cache directory \"%s\" is not a directory" % (self._path))


class RunnerStraitNeedConfigDir(RunnerStrait):

    def __init__(self, game_id):
        self._path = os.path.expanduser(os.path.join("~", ".config", game_id))

    def _prepare(self, context):
        if not os.path.exists(self._path):
            try:
                os.mkdir(self._path)
            except OSError as e:
                raise PrepareError("failed to create config directory \"%s\", %s" % (self._path, e))
        else:
            if not os.path.isdir(self._path):
                raise PrepareError("\"%s\" is not a directory" % (self._path))

        context.config_dirpath = self._path

    @property
    def config_dirpath(self):
        return self._path


class RunnerStraitHasConfigDir(RunnerStrait):

    def __init__(self, game_object):
        self._path = game_object.get_cache_dir()

    def _prepare(self, context):
        # game creates config directory itself
        if os.path.exists(self._path) and not os.path.isdir(self._path):
            raise PrepareError("game config directory \"%s\" is not a directory" % (self._path))
